<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑商品</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
</head>

<body>
    <div style="margin: 15px;padding: 15px;background-color: #fff;">
        <form class="layui-form">
            <input type="hidden" name="id" value="{$row['id']}">
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" value="{$row['title']}" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品图片</label>
                <div class="layui-input-inline">
                    <input type="text" name="img" value="{$row['img']}" class="layui-input">
                    <div class="layui-upload-list" style="position:relative;">{if $row['img']}<img src="{$row['img']}" alt="Preview" style="max-width:92px" onerror="this.parentNode.remove()" />{/if}</div>
                </div>
                <button type="button" class="layui-btn layui-btn-primary" id="upload-btn">上传</button>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">品牌</label>
                <div class="layui-input-inline">
                    <select name="brand_id">
                        <option></option>
                        {foreach $brands as $brand}
                        <option value="{$brand['id']}" {if $brand['id']==$row['brand_id']} selected{/if}>
                            {$brand['title']}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">原价</label>
                <div class="layui-input-inline">
                    <input type="number" name="orig_price" value="{$row['orig_price']}" lay-verify="number"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">最低价</label>
                <div class="layui-input-inline">
                    <input type="number" name="min_price" value="{$row['min_price']}" lay-verify="required|number"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit lay-filter="save">保存</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use(['form', 'upload'], function () {
            var form = layui.form
                , upload = layui.upload
                , $ = layui.jquery;

            //获取应用、控制器和方法
            var module = '{:app("http")->getName()}'
                , controller = '{$Request.controller}'
                , action = '{$Request.action}';

            //商品图片上传
            upload.render({
                elem: '#upload-btn',
                url: '/' + module + '/files/upload_img',
                data: {},
                before: function (obj) {
                    layer.load();
                },
                done: function (res, index, upload) {
                    layer.closeAll('loading');
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $(this.item).prev().find('input[class="layui-input"]').val(res.data.src)
                    $(this.item).prev().find('.layui-upload-list').html('<img src="' + res.data.src + '" alt="Preview" style="max-width:92px" onerror="this.parentNode.remove()" />');
                },
                error: function (index, upload) {
                    layer.closeAll('loading');
                }
            });

            form.on('submit(save)', function (obj) {
                $.post('/' + module + '/' + controller + '/save_edit', obj.field, function (res) {
                    if (res.code) return layer.msg(res.msg, { icon: 2 });
                    parentIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(parentIndex);
                    parent.layer.msg(res.msg, { icon: 1 });
                    parent.layui.table.reload('productTable');
                }, 'json');
            });
        });
    </script>
</body>

</html>